<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class BaseTableBase extends Base {
	var $table;
	var $pkey;
	var $field;
	var $id;
	var $row;
	
	//constructor
	function BaseTableBase($table, $pkey, $field, $id = 0) {
		$this->table = $table;
		$this->pkey = $pkey;
		$this->field = $field;
		$this->id = $id;
		
		parent::Base();
	}
	
	//methods
	function isOnly($field, $value, $id = 0, $where = '') {
		$query = "select count(*) as count from " . $this->table . " where " . $field . " = '" . $value . "'";
		if( $id > 0 ) {
			$query .= " and " . $this->pkey . " <> '" . (int)$id . "'";
		}
		if( $where != '' ) {
			$query .= " and " . $where;
		}
		
		return $this->adb->checkCount($query);
	}
	
	function num($field, $value, $id = 0, $where = '') {
		$query = "select count(*) as count from " . $this->table . " where " . $field . " = '" . $value . "'";
		if( $id > 0 ) {
			$query .= " and " . $this->pkey . " <> '" . (int)$id . "'";
		}
		if( $where != '' ) {
			$query .= " and " . $where;
		}
		
		return $this->adb->getCount($query);
	}
	
	function bind($id = 0) {
		if( $id == 0 ) {
			$id = $this->id;
		}
		$query = "select * from " . $this->table . " where " . $this->pkey . " = '" . (int)$id . "'";
		$this->row = $this->adb->getRow($query);
	}
	
	function row($id = 0) {
		if( $id == 0 ) {
			$id = $this->id;
		}
		$query = "select * from " . $this->table . " where " . $this->pkey . " = '" . (int)$id . "'";
		$this->row = $this->adb->getRow($query);
		
		return $this->row;
	}
	
	function get($field = '', $id = 0) {
		if( $field == '' ) {
			$field = $this->field;
		}
		if( $id == 0 ) {
			$id = $this->id;
		}
		$query = "select " . $field . " from " . $this->table . " where " . $this->pkey . " = '" . (int)$id . "'";
		
		$row = $this->adb->getRow($query);
		
		return $row[$field];
	}
	
	function update($field, $value, $id = 0) {
		if( $field == '' ) {
			$field = $this->field;
		}
		if( $id == 0 ) {
			$id = $this->id;
		}
		$query = "update " . $this->table . " set " . $field . " = '" . $value . "' where " . $this->pkey . " = '" . (int)$id . "'";
		
		return $this->adb->update($query);
	}
	
	function delete($id) {
		$query = "delete from " . $this->table . " where " . $this->pkey . " = '" . (int)$id . "'";
		
		return $this->adb->delete($query);
	}
}
//
?>